﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;
using System.Data;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace MarkControle.Models
{
    public class tarefa:IComparable<tarefa>
    {
        private int a, b, c;
        [Required]
        [Range(2012,2015)]
        public int C
        {
            get { return c; }
            set { c = value; }
        }
        [Required]
        [Range(1,12)]
        public int B
        {
            get { return b; }
            set { b = value; }
        }
        [Required]
        [Range(1,31)]
        public int A
        {
            get { return a; }
            set { a = value; }
        }

        private string enca1, enca2, enca3;
        
        public string Enca3
        {
            get { return enca3; }
            set { enca3 = value; }
        }
      
        public string Enca2
        {
            get { return enca2; }
            set { enca2 = value; }
        }
        [Required]
        public string Enca1
        {
            get { return enca1; }
            set { enca1 = value; }
        }
        private DateTime prazo=new DateTime();
        private enum Status { cumprida = 3, adiada = 1, atrasada = 2, pendente = 0 };
        private int status;   
        private string comentarios;
        private string informacoes;
        private string[] encarregados = new string[3];
        private DateTime datarequisicao=new DateTime();
        private int nadd;
        private int priori;


        

        public tarefa(DateTime prazo, string comentarios, string informacoes, string[] encarregados,int status,DateTime requisi,int nadd,int priori)
        {
            this.prazo = prazo;
            this.comentarios = comentarios;
            this.informacoes = informacoes;
            this.status = status;
            datarequisicao = requisi;
            this.encarregados[0] = encarregados[0];
            this.encarregados[1] = encarregados[1];
            this.encarregados[2] = encarregados[2];
            this.nadd = nadd;
            this.priori = priori;

        }
        public tarefa() { }
        public DateTime Datarequisicao
        {
            get { return datarequisicao; }
            set { datarequisicao = value; }
         
        }

        public int Nadd
        {
            get { return nadd; }
            set
            {
                Updatebanco(prazo, status, comentarios, informacoes, encarregados, datarequisicao, value+nadd,priori);  
                nadd = nadd+value; }
        }
        [Range(0,2)]
        public int Priori
        {
            get { return priori; }
            set
            {
                Updatebanco(prazo, status, comentarios, informacoes, encarregados, datarequisicao, nadd, value); 
                priori = value; }
        }

        public int Status1
        {
            get { return status; }
            set
            {

                Updatebanco(prazo, value, comentarios, informacoes, encarregados, datarequisicao, nadd, priori);
                status = value;
            }
        }


        public string[] Encarregados
        {
            get { return encarregados; }
            set {
                Updatebanco(prazo, status, comentarios, informacoes, value, datarequisicao, nadd, priori);
                
                encarregados = value; }
        }


        public DateTime Prazo
        {

            get { return prazo; }
            set {
                Updatebanco(value, status, comentarios, informacoes, encarregados, datarequisicao, nadd, priori);
                prazo = value;
                
                 }

        }

        [StringLength(240)]
        [Required]
        public string Comentarios
        {
            set
            {
                Updatebanco(prazo, status, value, informacoes, encarregados, datarequisicao, nadd, priori);


                comentarios = value;
            }
            get { return comentarios; }
            
        }
        [StringLength(240)]
        [Required]
        public string Informacoes
        {
            get { return informacoes; }
            set
            {
                Updatebanco(prazo, status, comentarios, value, encarregados, datarequisicao,nadd,priori);


                informacoes = value;
            }
        }



        public bool Adicionabanco()
        {
            bool sucess = true;
            string urlConn = "Database=markcontrole;Data Source=mysql01.markcontrole.hospedagemdesites.ws;User Id=markcontrole;Password=ivan120869; pooling=false";

            // Create objeto Connection
            MySqlConnection conn = new MySqlConnection(urlConn);

            // Open Connection
            try
            {
                conn.Open();
            }
            catch (MySqlException e)
            {
                Console.WriteLine("Error connection by: {0}", e.Message);
            } // end try

            // Validate connection
            if (conn.State.Equals(ConnectionState.Open))
            {
                MySqlTransaction transaction = null;
                try
                {
                    // Open transaction

                    transaction = conn.BeginTransaction();

                    // Create query e Command
                    StringBuilder sql = new StringBuilder();
                    sql.Append("INSERT INTO tarefas (PRAZO,INFORMACOES,ENCARREGADO1,ENCARREGADO2,ENCARREGADO3,COMENTARIOS,REQUISICAO,STATUS,PRIORIDADE,NAD) ");
                    sql.Append("VALUES(@pra, @inf,@enc1,@enc2,@enc3,@com,@datreq,@sta,@priori,@nad)");
                    MySqlCommand command = new MySqlCommand(sql.ToString(), conn);
                    
                    // Add Parameters
                    command.Parameters.AddWithValue("@pra", prazo.Date);
                    command.Parameters.AddWithValue("@inf", informacoes);
                    command.Parameters.AddWithValue("@enc1", encarregados[0]);
                    command.Parameters.AddWithValue("@enc2", encarregados[1]);
                    command.Parameters.AddWithValue("@enc3", encarregados[2]);                    
                    command.Parameters.AddWithValue("@com", comentarios);
                    command.Parameters.AddWithValue("@datreq", datarequisicao);
                    command.Parameters.AddWithValue("@sta", status);
                    command.Parameters.AddWithValue("@priori", priori);
                    command.Parameters.AddWithValue("@nad", nadd);

                    // Insert customer
                    command.ExecuteNonQuery();


                    // Commit transaction
                    transaction.Commit(); 
                }
                catch (MySqlException e)
                {
                    transaction.Rollback();
                    sucess = false;
                }
                finally
                {
                    // Close connection
                    conn.Close();
                } // end try
            }





            return sucess;
        }
       

        public bool Deletebanco()
        {


            bool sucess = true;
            string urlConn = "Database=markcontrole;Data Source=mysql01.markcontrole.hospedagemdesites.ws;User Id=markcontrole;Password=ivan120869; pooling=false";

            // Create objeto Connection
            MySqlConnection conn = new MySqlConnection(urlConn);

            // Open Connection
            try
            {
                conn.Open();
            }
            catch (MySqlException e)
            {
                Console.WriteLine("Error connection by: {0}", e.Message);
            } // end try

            // Validate connection
            if (conn.State.Equals(ConnectionState.Open))
            {
                MySqlTransaction transaction = null;
                try
                {
                    // Open transaction
                    transaction = conn.BeginTransaction();

                    // Create query e Command
                    StringBuilder sql = new StringBuilder();
                    sql.Append("DELETE FROM tarefas WHERE PRAZO=@pra AND INFORMACOES=@inf AND STATUS=@stat");
                    MySqlCommand command = new MySqlCommand(sql.ToString(), conn);

                    // Add Parameters
                    command.Parameters.AddWithValue("@pra", prazo.Date);
                    command.Parameters.AddWithValue("@inf", informacoes);                
                    command.Parameters.AddWithValue("@stat", status);
                    

                    // Insert customer
                    command.ExecuteNonQuery();


                    // Commit transaction
                    transaction.Commit();
                }
                catch (MySqlException e)
                {
                    transaction.Rollback();
                    sucess = false;
                }
                finally
                {
                    // Close connection
                    conn.Close();
                } // end try
            }





            return sucess;





        }

        public void Updatebanco(DateTime nprazo, int nstatus, string ncomentario, string ninformacoes, string[] nencarregados, DateTime nrequisicao,int nnadd,int npriori)
        {



            string urlConn = "Database=markcontrole;Data Source=mysql01.markcontrole.hospedagemdesites.ws;User Id=markcontrole;Password=ivan120869; pooling=false";
            MySqlConnection conn = new MySqlConnection(urlConn);

            try
            {
                conn.Open();
            }
            catch (MySqlException e)
            {
                Console.WriteLine("Error connection by: {0}", e.Message);
            } // end try



            if (conn.State.Equals(ConnectionState.Open))
            {
                MySqlTransaction transaction = null;
                try
                {
                    // Open transaction
                    transaction = conn.BeginTransaction();

                    String sql = "UPDATE tarefas SET PRAZO=@npra,INFORMACOES=@ninf,ENCARREGADO1=@nenc1,ENCARREGADO2=@nenc2,ENCARREGADO3=@nenc3,COMENTARIOS=@ncom,REQUISICAO=@ndatreq,STATUS=@nsta,PRIORIDADE=@npriori,NAD=@nnadd WHERE PRAZO=@pra AND INFORMACOES=@inf AND ENCARREGADO1=@enc1 AND ENCARREGADO2=@enc2 AND ENCARREGADO3=@enc3 AND COMENTARIOS=@com AND REQUISICAO=@datreq AND STATUS=@sta";

                    MySqlCommand command = new MySqlCommand(sql, conn);


                    command.Parameters.AddWithValue("@npra", nprazo.Date);
                    command.Parameters.AddWithValue("@nenc1", nencarregados[0]);
                    command.Parameters.AddWithValue("@nenc2", nencarregados[1]);
                    command.Parameters.AddWithValue("@nenc3", nencarregados[2]);
                    command.Parameters.AddWithValue("@ninf", ninformacoes);
                    command.Parameters.AddWithValue("@ncom", ncomentario);
                    command.Parameters.AddWithValue("@ndatreq", nrequisicao.Date);
                    command.Parameters.AddWithValue("@nsta", nstatus);
                    command.Parameters.AddWithValue("@nnadd", nnadd);
                    command.Parameters.AddWithValue("@npriori", npriori);


                    command.Parameters.AddWithValue("@pra", prazo.Date);
                    command.Parameters.AddWithValue("@enc1", encarregados[0]);
                    command.Parameters.AddWithValue("@enc2", encarregados[1]);
                    command.Parameters.AddWithValue("@enc3", encarregados[2]);
                    command.Parameters.AddWithValue("@inf", informacoes);
                    command.Parameters.AddWithValue("@com", comentarios);
                    command.Parameters.AddWithValue("@datreq", datarequisicao.Date);
                    command.Parameters.AddWithValue("@sta", status);



                    command.ExecuteNonQuery();



                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                }
                finally
                {
                    conn.Close();
                }




            }

        }


        public override bool Equals(object o)
        {
            bool valid = false;
            if (o != null && o is tarefa)
            {
                tarefa refa = (tarefa)o;


                if (this.informacoes.Equals(refa.Informacoes, StringComparison.OrdinalIgnoreCase))
                {
                    valid = true;
                }
            }
            return valid;
        }

        public int CompareTo(tarefa TarefatoCompare)
        {
            if (this.Status1 < TarefatoCompare.Status1)
                return 1;
            else if (this.Status1 > TarefatoCompare.Status1)
                return -1;
            else
                return DateTime.Compare(TarefatoCompare.Prazo, this.Prazo);

        }





    }

}